﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;

namespace sydfdl.TMS.BLL
{
    public class BLLT_E_Truck : T_Base
    {

        public T_Base<T_E_Truck> GetList(string xml)
        {
            T_Base<T_E_Truck> List = new T_Base<T_E_Truck>();
            #region
            SqlParameter[] para = {new SqlParameter("@para",SqlDbType.Xml,-1)};
            para[0].Value = xml;
            using (DataSet ds = SqlHelper.ExecuteProcedureToDataSet( @"P_E_GetTruck",para,""))
            {
                List<T_E_Truck> inner = new List<T_E_Truck>();
                if (ds.Tables.Count>0)
                    List.total = ds.Tables[0].Rows.Count;
                if (ds.Tables[0].Rows.Count>0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        T_E_Truck dto = new T_E_Truck();
                        dto.TruckID = new Guid(dr[1].ToString());
                        dto.TruckNum = dr[2].ToString();
                        dto.Brand = dr[3].ToString();
                        dto.Model = dr[4].ToString();
                        dto.MaxLong = Convert.ToDouble(dr[5].ToString());
                        dto.MaxWidth = Convert.ToDouble(dr[6].ToString());
                        dto.MaxHeight = Convert.ToDouble(dr[7].ToString());
                        dto.MaxLoadWeight = dr[8].ToString();
                        dto.MaxLoadVolume = dr[9].ToString();
                        dto.InsuranceBillNum = dr[10].ToString();
                        dto.EngineNum = dr[11].ToString();
                        dto.TransportNum = dr[12].ToString();
                        dto.IsCompany = dr[13].ToString();
                        dto.ContractNum = dr[14].ToString();
                        dto.Owner = dr[15].ToString();
                        dto.OwnerTel = dr[16].ToString();
                        dto.LinkMan = dr[17].ToString();
                        dto.LinkTel = dr[18].ToString();
                        dto.CreateUser = dr[19].ToString();
                        if (!string.IsNullOrEmpty(dr[20].ToString()))
                        {
                            dto.CreateTime =  Convert.ToDateTime(dr[20].ToString());
                        }

                        inner.Add(dto);
                    }
                    List.data = inner;
                }
            }
            #endregion
            return List;
        }

        /// <summary>
        /// 数据操作
        /// </summary>
        /// <param name="xml">第一个是增加，第二个是修改，第三个是删除</param>
        /// <returns></returns>
        public string Save(params IEnumerable<T_E_Truck>[] xml)
        {
            string strResult = "";
            string addXML = EFHelp<T_E_Truck>.GetXML(xml[0]);
            string editXML = EFHelp<T_E_Truck>.GetXML(xml[1]);
            string delXML = EFHelp<T_E_Truck>.GetXML(xml[2]);
            SqlParameter[] para
                =  {
                       new SqlParameter("@addXmL",SqlDbType.Xml,-1),
                       new SqlParameter("@editXML",SqlDbType.Xml,-1),
                       new SqlParameter("@delXML",SqlDbType.Xml,-1),
                       new SqlParameter("@userXML",SqlDbType.Xml,-1),
                       new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                    };
            para[0].Value = addXML;
            para[1].Value = editXML;
            para[2].Value = delXML;
            para[3].Value = null;
            para[4].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_E_SaveTruck", para);
            strResult = para[4].Value.ToString();
            return strResult;
        }
    }
}
